//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.exception.FileFormatException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.UUID;

@Api(
        tags = {"文件上传接口"}
)
@RestController
@RequestMapping({"/file"})
public class FilesUploadController {
    @Value("${application.upload-path}")
    private String fileDirectory;

    public FilesUploadController() {
    }

    @ApiOperation("头像上传")
    @PostMapping({"/upload/{type}"})
    public Result<String> upload(@PathVariable("type") String type, @RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
        System.out.println("请求图片");
        String originalFilename = file.getOriginalFilename();
        String tail = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!tail.equals(".jpg") & !tail.equals(".gif") & !tail.equals(".png") & !tail.equals(" eg") & !tail.equals(".webp") & !tail.equals(".mp4")) {
            throw new FileFormatException("请传入正确格式文件");
        } else {
            String header = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName = header + tail;
            String targetFileLocation = this.fileDirectory + File.separator + type;
            File file1 = new File(targetFileLocation);
            if (!file1.exists()) {
                file1.mkdirs();
            }

            String targetFileName = targetFileLocation + File.separator + newFileName;
            File targetFile = new File(targetFileName);
            if (!targetFile.exists()) {
                targetFile.createNewFile();
            }

            file.transferTo(targetFile);
            response.addHeader("Content-Disposition", "filename=" + newFileName);
            return new Result(true, 20000, "上传成功", newFileName);
        }
    }

    @ApiOperation("资料上传")
    @PostMapping({"/uploadInfo/{type}"})
    public Result<String> uploadInfo(@PathVariable("type") String type, @RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String tail = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!tail.equals(".jpg") & !tail.equals(".gif") & !tail.equals(".png") & !tail.equals(" eg") & !tail.equals(".webp") & !tail.equals(".mp4")) {
            throw new FileFormatException("请传入正确格式文件");
        } else {
            String header = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName = header + tail;
            String targetFileLocation = this.fileDirectory + File.separator + type;
            File file1 = new File(targetFileLocation);
            if (!file1.exists()) {
                file1.mkdirs();
            }

            String targetFileName = targetFileLocation + File.separator + newFileName;
            File targetFile = new File(targetFileName);
            if (!targetFile.exists()) {
                targetFile.createNewFile();
            }

            file.transferTo(targetFile);
            response.addHeader("Content-Disposition", "filename=" + newFileName);
            return new Result(true, 20000, "上传成功", newFileName);
        }
    }
}
